const express = require('express')
const router = express.Router()
const db = require('../db')
const consts = require('../../consts/Consts')
/**
 * @apiDefine mark web航标管理
 */

/**
 * @api {get} http://127.0.0.1:3658/mark/getNavigationTypeList 获取航标类型列表
 * @apiDescription 获取航标类型列表
 * @apiName getNavigationTypeList
 * @apiGroup mark
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getNavigationTypeList', (req, res, err) => {
  db.MarkDb.getNavigationTypeList((err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})

/**
 * @api {post} http://127.0.0.1:3658/mark/addNavigationType 添加航标类型
 * @apiDescription 添加航标类型
 * @apiName addNavigationType
 * @apiGroup mark
 * @apiParam (body) {String} type 航标类型
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addNavigationType', (req, res, next) => {
  const type = req.body.type
  db.MarkDb.addNavigationType(type, (err, raw) => {
    if (err) {
      console.log(err)
      if (err.errno === 19) {
        res.send({
          code: consts.RES_OK,
          data: 'RES_SQLITE_CONSTRAINT'
        })
      } else {
        res.end()
      }
    } else {
      res.send({
        code: consts.RES_OK,
        data: 'ok'
      })
    }
  })
})
/**
 * @api {get} http://127.0.0.1:3658/mark/delNavigationType 删除航标类型
 * @apiDescription 删除航标类型
 * @apiName delNavigationType
 * @apiGroup mark
 * @apiParam (body) {String} type 航标类型
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/delNavigationType', (req, res) => {
  const type = req.query.type
  console.log('delNavigationType: %s', type)
  db.MarkDb.deleteNavigationType(type, (err, row) => {
    if (err) throw err
    console.log('row: %o', row)
    res.send({
      code: consts.RES_OK,
      data: 'ok'
    })
  })
})

/**
 * @api {get} http://127.0.0.1:3658/mark/getAttributeList 获取航标属性列表
 * @apiDescription 获取航标属性列表
 * @apiName getAttributeList
 * @apiGroup mark
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getAttributeList', (req, res, err) => {
  console.log('mark-getAttributeList: ')
  db.MarkDb.getAttributeList((err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {post} http://127.0.0.1:3658/mark/addAttribute 添加航标属性
 * @apiDescription 添加航标属性
 * @apiName addAttribute
 * @apiGroup mark
 * @apiParam (body) {String} type 航标属性
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addAttribute', (req, res, next) => {
  const type = req.body.type
  db.MarkDb.addAttribute(type, (err, raw) => {
    if (err) {
      console.log(err)
      if (err.errno === 19) {
        res.send({
          code: consts.RES_OK,
          data: 'RES_SQLITE_CONSTRAINT'
        })
      } else {
        res.end()
      }
    } else {
      res.send({
        code: consts.RES_OK,
        data: 'ok'
      })
    }
  })
})

/**
 * @api {get} http://127.0.0.1:3658/mark/delAttribute 删除航标属性
 * @apiDescription 删除航标属性
 * @apiName delAttribute
 * @apiGroup mark
 * @apiParam (body) {String} type 航标属性
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/delAttribute', (req, res) => {
  const type = req.query.type
  db.MarkDb.deleteAttribute(type, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: 'ok'
    })
  })
})

/**
 * @api {get} http://127.0.0.1:3658/mark/getDivisionNameList 获取公司列表
 * @apiDescription 获取公司列表
 * @apiName getDivisionNameList
 * @apiGroup mark
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getDivisionNameList', (req, res, err) => {
  console.log('mark-getDivisionNameList: ')
  db.MarkDb.getDivisionNameList((err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {post} http://127.0.0.1:3658/mark/addDivisionName 添加公司
 * @apiDescription 添加公司
 * @apiName addDivisionName
 * @apiGroup mark
 * @apiParam (body) {String} type 公司
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addDivisionName', (req, res, next) => {
  const type = req.body.type
  db.MarkDb.addDivisionName(type, (err, raw) => {
    if (err) {
      console.log(err)
      if (err.errno === 19) {
        res.send({
          code: consts.RES_OK,
          data: 'RES_SQLITE_CONSTRAINT'
        })
      } else {
        res.end()
      }
    } else {
      res.send({
        code: consts.RES_OK,
        data: 'ok'
      })
    }
  })
})

/**
 * @api {get} http://127.0.0.1:3658/mark/delDivisionName 删除公司
 * @apiDescription 删除公司
 * @apiName delDivisionName
 * @apiGroup mark
 * @apiParam (body) {String} type 公司
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/delDivisionName', (req, res) => {
  const type = req.query.type
  db.MarkDb.deleteDivisionName(type, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: 'ok'
    })
  })
})
/**
 * @api {get} http://127.0.0.1:3658/mark/getChannelNameList 获取港口列表
 * @apiDescription 获取港口列表
 * @apiName getChannelNameList
 * @apiGroup mark
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getChannelNameList', (req, res, err) => {
  console.log('mark-getChannelNameList: ')
  db.MarkDb.getChannelNameList((err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})

/**
 * @api {post} http://127.0.0.1:3658/mark/addChannelName 添加港口
 * @apiDescription 添加港口
 * @apiName addChannelName
 * @apiGroup mark
 * @apiParam (body) {String} type 港口名
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addChannelName', (req, res, next) => {
  const type = req.body.type
  db.MarkDb.addChannelName(type, (err, raw) => {
    if (err) {
      console.log(err)
      if (err.errno === 19) {
        res.send({
          code: consts.RES_OK,
          data: 'RES_SQLITE_CONSTRAINT'
        })
      } else {
        res.end()
      }
    } else {
      res.send({
        code: consts.RES_OK,
        data: 'ok'
      })
    }
  })
})
/**
 * @api {get} http://127.0.0.1:3658/mark/delChannelName 删除港口
 * @apiDescription 删除港口
 * @apiName delChannelName
 * @apiGroup mark
 * @apiParam (body) {String} type 港口名
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/delChannelName', (req, res) => {
  const type = req.query.type
  db.MarkDb.deleteChannelName(type, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: 'ok'
    })
  })
})
/**
 * @api {get} http://127.0.0.1:3658/mark/getSeaRouteList 获取航道列表
 * @apiDescription 获取航道列表
 * @apiName getSeaRouteList
 * @apiGroup mark
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getSeaRouteList', (req, res, err) => {
  console.log('mark-getSeaRouteList: ')
  db.MarkDb.getSeaRouteList((err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {post} http://127.0.0.1:3658/mark/addSeaRoute 添加航道
 * @apiDescription 添加航道
 * @apiName addSeaRoute
 * @apiGroup mark
 * @apiParam (body) {String} type 航道名
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addSeaRoute', (req, res, next) => {
  const type = req.body.type
  db.MarkDb.addSeaRoute(type, (err, raw) => {
    if (err) {
      console.log(err)
      if (err.errno === 19) {
        res.send({
          code: consts.RES_OK,
          data: 'RES_SQLITE_CONSTRAINT'
        })
      } else {
        res.end()
      }
    } else {
      res.send({
        code: consts.RES_OK,
        data: 'ok'
      })
    }
  })
})
/**
 * @api {get} http://127.0.0.1:3658/mark/delSeaRoute 删除航道
 * @apiDescription 删除航道
 * @apiName delSeaRoute
 * @apiGroup mark
 * @apiParam (body) {String} type 航道名
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/delSeaRoute', (req, res) => {
  const type = req.query.type
  db.MarkDb.deleteSeaRoute(type, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: 'ok'
    })
  })
})
/**
 * @api {get} http://127.0.0.1:3658/mark/getAreaNameList 获取海区列表
 * @apiDescription 获取海区列表
 * @apiName getAreaNameList
 * @apiGroup mark
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getAreaNameList', (req, res, err) => {
  console.log('mark-getAreaNameList: ')
  db.MarkDb.getAreaNameList((err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {post} http://127.0.0.1:3658/mark/addAreaName 添加海区
 * @apiDescription 添加海区
 * @apiName addAreaName
 * @apiGroup mark
 * @apiParam (body) {String} type 海区名
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addAreaName', (req, res, next) => {
  const type = req.body.type
  db.MarkDb.addAreaName(type, (err, raw) => {
    if (err) {
      console.log(err)
      if (err.errno === 19) {
        res.send({
          code: consts.RES_OK,
          data: 'RES_SQLITE_CONSTRAINT'
        })
      } else {
        res.end()
      }
    } else {
      res.send({
        code: consts.RES_OK,
        data: 'ok'
      })
    }
  })
})
/**
 * @api {get} http://127.0.0.1:3658/mark/delAreaName 删除海区
 * @apiDescription 删除海区
 * @apiName delAreaName
 * @apiGroup mark
 * @apiParam (body) {String} type 海区名
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/delAreaName', (req, res) => {
  const type = req.query.type
  db.MarkDb.deleteAreaName(type, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: 'ok'
    })
  })
})

/**
 * @api {post} http://127.0.0.1:3658/mark/search 搜索航标
 * @apiDescription 搜索航标
 * @apiName search
 * @apiGroup mark
 * @apiParam (body) {String} NavigationType 航标类型
 * @apiParam (body) {String} ChannelName 港口类型
 * @apiParam (body) {String} SeaRoute 航道类型
 * @apiParam (body) {String} AtoNName 航标名

 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/search', (req, res, err) => {
  console.log('mark-search: %o', req.body)
  db.MarkDb.search(req.body, (err, list) => {
    if (err) throw err
    console.log('mark-search:%d', list.length)
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})

router.get('/page', (req, res, err) => {
  console.log('mark-page: ')
  db.MarkDb.page((err, users) => {
    if (err) throw err
    console.log(users)
    res.send(users)
    // res.format({
    //   json: () => {
    //     res.send(users)
    //   }
    // })
  })
})

module.exports = router
